/**
 * @file truebreathled.c
 * @brief 一个真正的呼吸灯。在task中调用
 * @author 2bam 
 * @version 1.0
 * @date 2023-08-27
 * @copyright Copyright (c) 2023  NULL
 * 
 * @change log:
 * Date           Version Author   Describe
 * 2023-08-27     1.0     2bam     first version
 */

/* Includes -------------------------------------------------------------------*/
#include <rtthread.h>
#include <rtdevice.h>
#include "stdbool.h"
#include <stdlib.h>
#include <stddef.h>
#include <drv_common.h>

#define DBG_TAG "truebreathled"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
/* Private define -------------------------------------------------------------*/
#define LEDONBOARD_PIN_NUM          GET_PIN(C,12)
#define LEDGREEN_PIN_NUM            GET_PIN(C,10)
#define LEDRED_PIN_NUM              GET_PIN(B,3)  
#define USER_KEY_NUM                GET_PIN(C,1)


#define LED_FALSH_PERIOD    10
#define LED_BREATH_PERIOD   200
/* Private typedef ------------------------------------------------------------*/
static rt_thread_t led_thread = RT_NULL;          //线程句柄
/* Private variables ----------------------------------------------------------*/

/* Private function prototypes ------------------------------------------------*/
static void user_led_task_entry(void *parameter);
static int user_led_task_create(void);
static void true_breath_led_linear(void);
static int user_led_task_create(void);

/**
 * @brief 呼吸灯初始化
 * @date 2023-08-27
 * @author 2bam 
 */
int breath_led_init(void)
{
    extern void user_button_action(void *args);
    /* 引脚为输出模式 */
    rt_pin_mode(LEDONBOARD_PIN_NUM, PIN_MODE_OUTPUT);
    /* 默认低电平 */
    rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);

    /* 引脚为输出模式 */
    rt_pin_mode(LEDGREEN_PIN_NUM, PIN_MODE_OUTPUT);
    /* 默认低电平 */
    rt_pin_write(LEDGREEN_PIN_NUM, PIN_LOW);

    /* 引脚为输出模式 */
    rt_pin_mode(LEDRED_PIN_NUM, PIN_MODE_OUTPUT);
    /* 默认低电平 */
    rt_pin_write(LEDRED_PIN_NUM, PIN_LOW);
    /* 创建线程 2023-09-Saturday 23:15:47 */
    user_led_task_create();

    /* 按键定义并设置中断 2023-08-Thursday 23:27:44 */
    rt_pin_mode(USER_KEY_NUM,PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(USER_KEY_NUM,PIN_IRQ_MODE_FALLING,user_button_action,RT_NULL);
    rt_pin_irq_enable(USER_KEY_NUM, PIN_IRQ_ENABLE);
    return 0;
}
INIT_APP_EXPORT(breath_led_init);


/**
 * @brief 处理LED亮度的入口函数
 * @param  parameter   My Param doc
 * @date 2023-09-09
 * @author 2bam 
 */
static void user_led_task_entry(void *parameter)
{
    while(1)
    {
        true_breath_led_linear();
        rt_thread_delay(1000);
    }
}


/**
 * @brief 创建处理LED亮度的线程
 * @return int 
 * @date 2023-09-09
 * @author 2bam 
 */
static int user_led_task_create(void)
{
    /*创建task*/
    led_thread = rt_thread_create("led_task",
                                    user_led_task_entry,
                                    NULL,
                                    1024,
                                    15,
                                    1000);
    if(led_thread == RT_NULL)
    {
        LOG_E("led_task create failed!\n");
    }
    else
    {
        LOG_D("led_task create success.\n");
    }

    /* 启动线程 */
    rt_thread_startup(led_thread);

    return 0;
}


/**
 * @brief 
 * @param  args        My Param doc
 * @date 2023-09-01
 * @author 2bam 
 */
void user_button_action(void *args)
{
    extern void led_green_toggle(void *args);
    
    rt_kprintf("user button action.\n");
    led_green_toggle(args);
}


/**
 * @brief 
 * @param  pinnum      GPIO编号
 * @date 2023-08-27
 * @author 2bam 
 */
void pin_toggle(uint32_t pinnum)
{
    rt_pin_write(pinnum, !rt_pin_read(pinnum));
}

/**
 * @brief 
 * @date 2023-08-27
 * @author 2bam 
 */
void led_green_toggle(void *args)
{
    pin_toggle(LEDGREEN_PIN_NUM);
}

/**
 * @brief 
 * @date 2023-08-27
 * @author 2bam 
 */
void led_red_toggle(void *args)
{
    pin_toggle(LEDRED_PIN_NUM);
}

/**
 * @brief 随时间线性变化的呼吸灯
 * @date 2023-08-27
 * @author 2bam 
 */
static void true_breath_led_linear(void)
{
    /* 从亮到灭的过程 2023-08-Sunday 17:09:26 */
    for(uint32_t breath_time=0;breath_time<10;breath_time++)
    {
        /* 循环10次，每次100ms */
        for(uint32_t i=0;i<10;i++)
        {
            rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
            rt_thread_mdelay(LED_FALSH_PERIOD-breath_time);  
            rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
            rt_thread_mdelay(breath_time);
        }
    }
    /* 最暗的时刻延时一段时间 2023-08-27-Sunday 17:14:00 */
    rt_thread_mdelay(500);

    /* 从灭到亮的过程 2023-08-27-Sunday 17:14:15 */
    for(uint32_t breath_time=0;breath_time<10;breath_time++)
    {
        /* 循环10次，每次100ms */
        for(uint32_t i=0;i<10;i++)
        {
            rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
            rt_thread_mdelay(LED_FALSH_PERIOD-breath_time);
            rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
            rt_thread_mdelay(breath_time);
        }
    }
    /* 最亮的时刻延时一段时间 2023-08-27-Sunday 17:19:01 */
    rt_thread_mdelay(700);
}


/**
 * @brief 可用于探究的非线性变化过程
 * @date 2023-08-27
 * @author 2bam 
 */
void true_breath_led_nonlinear(void)
{
    static uint32_t led_flash_state = 0;

    switch (led_flash_state)
    {
        case 0:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(LED_FALSH_PERIOD-led_flash_state);
            }
            led_flash_state = 1;
            break;

        case 1:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(LED_FALSH_PERIOD-led_flash_state);
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(led_flash_state);
            }
            led_flash_state = 2;
            break;

        case 2:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(LED_FALSH_PERIOD-led_flash_state);
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(led_flash_state);
            }
            led_flash_state = 3;
            break;

        case 3:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(LED_FALSH_PERIOD-led_flash_state);
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(led_flash_state);
            }
            led_flash_state = 4;
            break;

        case 4:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(LED_FALSH_PERIOD-led_flash_state);
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(led_flash_state);
            }
            led_flash_state = 5;
            break;

        case 5:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(LED_FALSH_PERIOD-led_flash_state);
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(led_flash_state);
            }
            led_flash_state = 6;
            break;

        case 6:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(LED_FALSH_PERIOD-led_flash_state);
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(led_flash_state);
            }
            led_flash_state = 7;
            break;

        case 7:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(LED_FALSH_PERIOD-led_flash_state);
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(led_flash_state);
            }
            led_flash_state = 8;
            break;

        case 8:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(LED_FALSH_PERIOD-led_flash_state);
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(led_flash_state);
            }
            led_flash_state = 9;
            break;

        case 9:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(LED_FALSH_PERIOD-led_flash_state);
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(led_flash_state);
            }
            led_flash_state = 10;
            break;

        case 10:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(LED_FALSH_PERIOD-led_flash_state);
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(led_flash_state);
            }
            /* 从亮到灭的过程结束，多延时500ms */
            rt_thread_mdelay(500);
            led_flash_state = 11;
            break;

        case 11:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(abs(LED_FALSH_PERIOD-led_flash_state));
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(LED_FALSH_PERIOD-abs(LED_FALSH_PERIOD-led_flash_state));
            }
            led_flash_state = 12;
            break;

        case 12:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(abs(LED_FALSH_PERIOD-led_flash_state));
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(LED_FALSH_PERIOD-abs(LED_FALSH_PERIOD-led_flash_state));
            }
            led_flash_state = 13;
            break;

        case 13:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(abs(LED_FALSH_PERIOD-led_flash_state));
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(LED_FALSH_PERIOD-abs(LED_FALSH_PERIOD-led_flash_state));
            }
            led_flash_state = 14;
            break;

        case 14:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(abs(LED_FALSH_PERIOD-led_flash_state));
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(LED_FALSH_PERIOD-abs(LED_FALSH_PERIOD-led_flash_state));
            }
            led_flash_state = 15;
            break;

        case 15:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(abs(LED_FALSH_PERIOD-led_flash_state));
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(LED_FALSH_PERIOD-abs(LED_FALSH_PERIOD-led_flash_state));
            }
            led_flash_state = 16;
            break;

        case 16:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(abs(LED_FALSH_PERIOD-led_flash_state));
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(LED_FALSH_PERIOD-abs(LED_FALSH_PERIOD-led_flash_state));
            }
            led_flash_state = 17;
            break;

        case 17:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(abs(LED_FALSH_PERIOD-led_flash_state));
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(LED_FALSH_PERIOD-abs(LED_FALSH_PERIOD-led_flash_state));
            }
            led_flash_state = 18;
            break;

        case 18:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(abs(LED_FALSH_PERIOD-led_flash_state));
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(LED_FALSH_PERIOD-abs(LED_FALSH_PERIOD-led_flash_state));
            }
            led_flash_state = 19;
            break;

        case 19:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(abs(LED_FALSH_PERIOD-led_flash_state));
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_HIGH);
                rt_thread_mdelay(LED_FALSH_PERIOD-abs(LED_FALSH_PERIOD-led_flash_state));
            }
            led_flash_state = 20;
            break;

        case 20:
            /* 循环10次，每次100ms */
            for(uint32_t i=0;i<10;i++)
            {
                rt_pin_write(LEDONBOARD_PIN_NUM, PIN_LOW);
                rt_thread_mdelay(abs(LED_FALSH_PERIOD-led_flash_state));
            }
            led_flash_state = 0;
            //* 从灭到亮结束，延时500ms */
            rt_thread_mdelay(500);
            break;
        
        default:
            break;
    }
}
